package com.eostoken.eos_wallet.ui.activity;

import android.graphics.Bitmap;
import android.view.View;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.eostoken.eos_wallet.R;
import com.eostoken.eos_wallet.base.BaseActivity;
import com.eostoken.eos_wallet.utils.ClipboardUtils;
import com.eostoken.eos_wallet.utils.WalletUtils;
import com.fredy.mvp.BasePresenter;
import com.fredy.mvp.BaseView;
import com.fredy.mvp.Model;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

import static com.eostoken.eos_wallet.utils.Constants.TO_TRANSFER_WALLET;

public class MoneyReceivingQRCodeActivity extends BaseActivity {

    @BindView(R.id.topbar)
    QMUITopBarLayout topbar;
    @BindView(R.id.qrCodeIv)
    AppCompatImageView qrCodeIv;
    @BindView(R.id.shareBtn)
    AppCompatTextView shareBtn;
    @BindView(R.id.copyBtn)
    AppCompatTextView copyBtn;
    @BindView(R.id.walletAddressTv)
    AppCompatTextView walletAddressTv;


    private Bitmap qrCodeBitmap;
    private String accountName;
    private Map<String, Object> map = new HashMap<>();

    @Override
    protected void initData() {
        accountName = WalletUtils.getCurrentAccountName();
        map.put("type", TO_TRANSFER_WALLET);
        map.put("accountName", accountName);
        String json = GsonUtils.toJson(map);
        qrCodeBitmap = QRCodeEncoder.syncEncodeQRCode(json, SizeUtils.getMeasuredHeight(qrCodeIv));
        qrCodeIv.setImageBitmap(qrCodeBitmap);
        walletAddressTv.setText(accountName);
    }

    @Override
    public void initView() {
        BarUtils.setStatusBarColor(this, ContextCompat.getColor(this, R.color.app_color_blue));
        BarUtils.setStatusBarLightMode(this, false);
        topbar.setTitle(StringUtils.getString(R.string.str_proceeds));
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public int getContentView() {
        return R.layout.activity_receipt_qrcode;
    }

    @OnClick({R.id.shareBtn, R.id.copyBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.shareBtn:
                break;
            case R.id.copyBtn:
                ClipboardUtils.copyText(accountName);


                ToastUtils.showShort(StringUtils.getString(R.string.str_copy_success));
                break;
        }
    }

    @Override
    public Model createModel() {
        return null;
    }

    @Override
    public BaseView createView() {
        return null;
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (qrCodeBitmap != null) {
            qrCodeBitmap.recycle();
            qrCodeBitmap = null;
        }
    }

    @Override
    public boolean isShowState() {
        return true;
    }
}
